$(document).ready(function() {
//	window.onresize = function() {
//		$('#canvas')[0].width = document.body.clientWidth;
//		$('#canvas')[0].height = document.body.scrollHeight - 60;
//	};
//	window.onresize();
	var canvas = document.getElementById("canvas");
	stage = new JTopo.Stage(canvas);
	showJTopoToobar(stage);
	var scene = new JTopo.Scene(stage);
	scene.background = "./img/mind/bg.png";

	var currentNode = null;
	
	var root = new JTopo.Node("主题");
	root.font = "italic bold 22px 微软雅黑";
	root.borderRadius = 18;
	root.fontColor = "0,0,0";
	root.textPosition = "Middle_Center";
	root.fillColor = "233,223,152";
	root.setSize(100, 60);
	root.setCenterLocation(canvas.width / 2, canvas.height / 2);
	scene.add(root);

	function newLink(nodeA, nodeZ) {
		var link = new JTopo.Link(nodeA, nodeZ);
		link.strokeColor = "255,255,255";
		link.paint = function(nodeA) {//绘制曲线
			if(null != this.nodeA && null != !this.nodeZ){
				var cx = (this.nodeA.cx + this.nodeZ.cx) / 2; //x轴
				var cy = (this.nodeA.cy + this.nodeZ.cy) / 2; //y轴
				cy += (this.nodeZ.cy - this.nodeA.cy) / 2; 
				nodeA.strokeStyle = "rgba(" + this.strokeColor + "," + this.alpha + ")";
				nodeA.lineWidth = this.lineWidth;
				nodeA.moveTo(this.nodeA.cx, this.nodeA.cy); 
				nodeA.quadraticCurveTo(cx, cy, this.nodeZ.cx, this.nodeZ.cy);
				nodeA.stroke();
			}
		}
		return link
	}
	
	function changeParent(node, x, y) {
		if (node == root) return null;
		for (var i = 0 ; i < scene.childs.length; i++ ) {
			var child = scene.childs[i];
			if (child !== node && !(!child instanceof JTopo.Node) && (!child.link || child.link.nodeA !== node)){
				var bound = child.getBound(); 
				bound.left -= 20;
				bound.right += 20;
				bound.top -= 20;
				bound.bottom += 20; 
				if(x > bound.left && x < bound.right &&  y > bound.top && y < bound.bottom){
					return child;
				}
			} 
		}
		return null
	}
	
	function createSubtopics(x, y, name) {
		var subtopics = new JTopo.Node(name);
		subtopics.borderRadius = 8;
		subtopics.setCenterLocation(x, y);
		subtopics.setSize(120, 40);
		subtopics.font = "italic 18px 微软雅黑";
		subtopics.textOffsetY = -2;
		subtopics.fontColor = "0,0,0";
		subtopics.textPosition = "Middle_Center";
		subtopics.fillColor = "164,197,192";
		scene.add(subtopics);
		var link = newLink(root, subtopics);
		subtopics.link = link;
		scene.add(link);
		var isDrag = false;//是否是拖曳
		var bottomNode = null;
		subtopics.mouseup(function(event) {
			if(isDrag){
				if(bottomNode){
					bottomNode.fillColor = bottomNode.fillColor_bak; 
					this.link.nodeA = bottomNode; 
					bottomNode = null;
				}	
				isDrag = false;
			}else{
				currentNode = this; 
				handler(event);
			}
		});
		subtopics.mousedrag(function(event) {
			isDrag = true;
			if(bottomNode){
				bottomNode.fillColor = bottomNode.fillColor_bak;
				bottomNode = null;
			}
			var node = changeParent(subtopics, event.x, event.y);
			if(node){
				bottomNode = node;
				bottomNode.fillColor_bak = bottomNode.fillColor;
				bottomNode.fillColor = "254,219,0";
			}
		});

		return subtopics;
	}
	
	function handler(event){
    	if(event.button == 2){//右键
    		$("#contextmenu").css({
                top: event.pageY,
                left: event.pageX
            }).show();
    	}
    }
	
	/* 右键菜单处理 */    
    $("#contextmenu a").click(function(){
        var text = $(this).text();
        
        if(text == '删除该节点'){
            scene.remove(currentNode);
            currentNode = null;
            $("#contextmenu").hide();
            return;
        }
        
        if(text == '撤销上一次操作'){
            currentNode.restore();
        }else{
            currentNode.save();
        }
        
        if(text == '更改颜色'){
            currentNode.fillColor = JTopo.util.randomColor();
        }else if(text == '顺时针旋转'){
            currentNode.rotate += 0.5;
        }else if(text == '逆时针旋转'){
            currentNode.rotate -= 0.5;
        }else if(text == '放大'){
            currentNode.scaleX += 0.2;
            currentNode.scaleY += 0.2;
        }else if(text == '缩小'){
            currentNode.scaleX -= 0.2;
            currentNode.scaleY -= 0.2;
        }
        $("#contextmenu").hide();
    });
	
	stage.click(function(event){
        if(event.button == 0){// 左键
            $("#contextmenu").hide();
        }
    });
    
    var textfield = $("#jtopo_textfield");
	scene.dbclick(function(event) {
		if (null == event.target) {
			createSubtopics(event.x, event.y, "分支主题");
		} else {
			var target = event.target;
			textfield.css({
				top: event.pageY,
				left: event.pageX - target.width / 2
			}).show().attr("value", target.text).focus().select(),
			target.text = "",
			textfield[0].JTopoNode = target;
		}
	});
	
//	scene.click(function(){//序列化
//		console.log(stage.toJson());
//	});
	
	$("#jtopo_textfield").blur(function() {
		textfield[0].JTopoNode.text = textfield.hide().val()
	});
	
});